﻿using Admin.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Security;

namespace Admin.Controllers
{
    public class LoginController : ApiController
    {
        [HttpPost]
        [AllowAnonymous]
        public IHttpActionResult Login([FromBody] Users users)
        {
            using (CRMEntities db = new CRMEntities())
            {
                var list = db.Users.SingleOrDefault(p => p.LoginName == users.LoginName);
                if (list != null)
                {
                    if (list.IsLockout == 1)
                    {
                        return Json(new { code = -1, msg = "该用户已经锁定" });
                    }
                    else
                    {
                        if (users.Password == list.Password)
                        {
                            string token = list.Id.ToString();
                            FormsAuthentication.SetAuthCookie(token, true);
                            return Json(new { code = 0, msg = "登录成功！", token = token });
                        }
                        else
                        {
                            return Json(new { code = -2, msg = "密码错误！" });
                        }
                    }
                }
                else
                {
                    return Json(new { code = -3, msg = "用户名不存在" });
                }

            }
        }
        [HttpPost]
        [AllowAnonymous]
        public IHttpActionResult Edit([FromBody] Users user)
        {
            using (CRMEntities db = new CRMEntities())
            {
                var list = db.Users.FirstOrDefault(p => p.Id == user.Id);
                if (list.Password == user.JPwd)
                {
                    list.Password = user.XPwd;
                    int a = db.SaveChanges();
                    if (a > 0)
                    {
                        return Json(new { code = 0, msg = "修改成功" });
                    }
                    else
                    {
                        return Json(new { code = -2, msg = "修改失败" });
                    }
                }
                else
                {
                    return Json(new { code = -1, msg = "旧密码错误" });
                }
            }
        }
        [HttpPost]
        [AllowAnonymous]
        public IHttpActionResult menus([FromBody] Users user)
        {
            using (CRMEntities db = new CRMEntities())
            {
                //获取登录用户Id
                Users userId = db.Users.SingleOrDefault(p => p.Id == user.Id);
                //根据用户id获取对应用户角色Id
                var rolesId_list = db.UserRoles.Where(p => p.UserId == userId.Id).ToList();
                var list1 = rolesId_list.Select(p => new
                {
                    RoleId = p.RoleId
                }).ToList();
                //用角色id去获得对应模块id
                List<Modules> data1 = new List<Modules>();
                List<Modules> data2 = new List<Modules>();
                foreach (var item in list1)
                {
                    var moduleId_list = db.RoleModules.Where(p => p.RoleId == item.RoleId).ToList();
                    var list2 = moduleId_list.AsEnumerable();

                    foreach (var itema in list2)
                    {
                        var list3 = db.Modules.FirstOrDefault(p => p.Id == itema.ModuleId);
                        if (list3.ParentId == 0)
                        {
                            data1.Add(list3);
                        }
                        else
                        {
                            data2.Add(list3);
                        }
                    }
                    //获取父级模块
                }

                var list5 = data1.Select(p => new
                {
                    Id = p.Id,
                    Name = p.Name,
                    ParentId = p.ParentId,
                    Path = p.Path,
                    Weight = p.Weight
                }).Distinct().ToList();
                var list6 = data2.Select(p => new
                {
                    Id = p.Id,
                    Name = p.Name,
                    ParentId = p.ParentId,
                    Path = p.Path,
                    Weight = p.Weight
                }).Distinct().ToList();
                return Json(new { code = 0, data1 = list5, data2 = list6, msg = "请求成功！" });
            }
        }
    }
}
